package server;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * AuctionManager: Manages Subjects and Bids of the Users
 * 
 * @author Fleischhacker
 *
 */
public class AuctionManager {
	/**
	 * Static ID-Management
	 */
	private static int ID_COUNT = 0;
	public static int GET_ID(){
		return ++ID_COUNT;
	}
	/**
	 * Collection for the Auctions
	 * 	Vector is already synchronized, so there are less problems if more 
	 * 	Actions are performed on the same Auction
	 */
	private Vector<Auction> auctions;
	
	public AuctionManager(){
		auctions = new Vector<Auction>();
	}
	
	/**
	 * creates an auction and adds it to the container
	 */
	public Auction createAuction(String _description, User _owner, int _time){
		auctions.add(new Auction(AuctionManager.GET_ID(),_description,_owner,_time));
		return auctions.lastElement();
	}
	
	/**
	 * Returns the Auction by the given ID
	 * @param id - int
	 * @return Auction - if ID is valid
	 * 			null otherwise
	 */
	public Auction getAuctionById(int id){
		for(Auction a : auctions){
			if(a.getID() == id)
				return a;
		}
		return null;
	}
	
	/**
	 * Returns the Auction by the given description
	 * @param descr - String
	 * @return Auction - if string is valid
	 * 			null otherwise
	 */
	public Auction getAuctionByDescription(String descr){
		return null;
	}
	
	/**
	 * Returns a List of the (still valid) Auctions sorted by ID
	 * @return
	 */
	public List<Auction> listAuctions(){
		ArrayList<Auction> list = new ArrayList<Auction>();
		for (Auction a : auctions){
			if(a.isActive())
				list.add(a);
		}
		return list;
	}
}
